package com.plantpoint.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.plantpoint.dao.PlantEntryDao;
import com.plantpoint.dao.UserDao;
import com.plantpoint.entity.bean.CommonResult;
import com.plantpoint.entity.bean.PageResult;
import com.plantpoint.entity.dto.PlantAddDTO;
import com.plantpoint.entity.dto.PlantUpdateDTO;
import com.plantpoint.entity.pojo.PlantEntry;
import com.plantpoint.entity.pojo.User;
import com.plantpoint.util.DateTimeTransferUtil;
import com.plantpoint.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlantEntryServiceImpl extends ServiceImpl<PlantEntryDao, PlantEntry> {
    private PlantEntryDao plantEntryDao;
    private IdWorker idWorker;
    private UserDao userDao;

    public PlantEntryServiceImpl(PlantEntryDao plantEntryDao, IdWorker idWorker, UserDao userDao) {
        this.plantEntryDao = plantEntryDao;
        this.idWorker = idWorker;
        this.userDao = userDao;
    }

    public PageResult<PlantEntry> findAllPlant(Integer cp,Integer ps){
        Page<Object> page = PageHelper.startPage(cp, ps);
        List<PlantEntry> plantEntries = plantEntryDao.selectList(null);
        return new PageResult<>(page.getPages(),page.getTotal(),plantEntries);
    }

    public Boolean save(String openid,PlantAddDTO plantAddDTO){
        //判断同名是否存在同名植物
        String scientific_name = plantAddDTO.getScientificName();
        QueryWrapper<PlantEntry> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("scientific_name",scientific_name);
        PlantEntry one = plantEntryDao.selectOne(objectQueryWrapper);
        if (one!=null){
            return false;
        }

        //如果不存在，则创建新植物
        //获取id
        String plantId = idWorker.nextId() + "";
        //创建时间与更新时间
        Long nowTimeStamp = DateTimeTransferUtil.getNowTimeStamp();
        //创建者
        String creator_openid = openid;
        //查询创建者
        User creator = userDao.selectOne(new QueryWrapper<User>().eq("openid", creator_openid));
        //创建新植物，将信息填入新植物
        PlantEntry plantEntry = new PlantEntry();
        BeanUtils.copyProperties(plantAddDTO,plantEntry);
        plantEntry.setId(plantId);
        plantEntry.setCreateTime(nowTimeStamp);
        plantEntry.setUpdateTime(nowTimeStamp);
        plantEntry.setCreatorOpenid(creator.getOpenid());
        plantEntry.setImagesId(plantId);
        plantEntry.setCreatorName(creator.getNickName());
        //持久化新植物
        plantEntryDao.insert(plantEntry);
        return true;
    }

    public Boolean updateById(PlantUpdateDTO plantUpdateDTO){
        //判断植物是否存在
        String scientific_name = plantUpdateDTO.getScientificName();
        QueryWrapper<PlantEntry> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("scientific_name",scientific_name);
        PlantEntry plant = plantEntryDao.selectOne(objectQueryWrapper);
        if (plant==null){
            return false;
        }
        //如将植物的数据更新
        BeanUtil.copyProperties(plantUpdateDTO,plant,true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true).setIgnoreProperties(""));
        plantEntryDao.updateById(plant);
        return true;
    }
}
